package com.sky.happygos.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;
import android.widget.Toast;

import com.sky.happygos.R;
import com.sky.happygos.base.BaseA;
import com.sky.happygos.common.LoadView;
import com.sky.happygos.common.YiChart.YiChart;
import com.sky.happygos.event.ChartOrderDataE;
import com.sky.happygos.event.ChartOrderSimpleE;
import com.sky.happygos.model.Order;
import com.sky.happygos.present.OrderP;
import com.sky.happygos.utils.DateUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by qqqqqqq on 2017/4/23.
 */
@EActivity(R.layout.activity_statistics)
public class StatisticsA extends BaseA {

    @ViewById(R.id.tv_all_order_num)
    TextView allNum;
    @ViewById(R.id.tv_all_order_price)
    TextView allPrice;
    @ViewById(R.id.tv_today_order_num)
    TextView todayNum;
    @ViewById(R.id.tv_today_order_price)
    TextView todayPrice;

    @ViewById(R.id.chart_month)
    YiChart yiChartMonth;

    @ViewById(R.id.chart_year)
    YiChart yiChartYear;

    @ViewById(R.id.loadView)
    LoadView loadView;

    @Bean
    OrderP orderP;

    @Click(R.id.header_left)
    public void back(){
        finish();
    }

    @AfterViews
    public void init(){

        loadView.start();
        orderP.getChartSimple(0);
        orderP.getChartSimple(1);
        orderP.getChartData(0);
        orderP.getChartData(1);


        List<String> titles = new ArrayList<>();
        int month = new Date().getMonth();
        for(int i = 0;i < 30;i++){
            String title = month +"-"+(i+1);
            titles.add(title);
        }

        yiChartMonth.setCount(titles);

        List<String> yearTitles = new ArrayList<>();
        for(int i = 0;i < 12;i++){
            String title = (i+1)+"月";
            yearTitles.add(title);
        }

//        List<Integer> yearDatas = new ArrayList<>();
//        yearDatas.add(new Integer(456));
//        yearDatas.add(new Integer(324));
//        yearDatas.add(new Integer(865));
//        yearDatas.add(new Integer(23));

        yiChartYear.setCount(yearTitles);

//        yiChartYear.setData(yearDatas);
    }

    @Subscribe
    public void chartSimpleResult(ChartOrderSimpleE e){
        loadView.dismiss();
        if(e.isSuccess){
            if(e.type == 0){
                todayNum.setText(e.data.count+"");
                todayPrice.setText(String.format("%.2f", e.data.price));
            }else{
                allNum.setText(e.data.count+"");
                allPrice.setText(String.format("%.2f", e.data.price));
            }
        }else{
            Toast.makeText(this,"请求失败",Toast.LENGTH_SHORT).show();
        }
    }

    @Subscribe
    public void chartDataResult(ChartOrderDataE e){
        loadView.dismiss();
        if(e.isSuccess){
            if(e.type == 0){
                List<Float> datas = processData(e.data,30);
                yiChartMonth.setData(datas);
            }else{
                List<Float> datas = processData(e.data,12);
                yiChartYear.setData(datas);
            }
        }else{
            Toast.makeText(this,"请求失败",Toast.LENGTH_SHORT).show();
        }
    }

    public List<Float> processData(List<Order> orders,int count){
        List<Float> datas = new ArrayList<>();
        for (int i = 0;i < count;i++){
            datas.add(new Float(0));
        }
        for (Order order : orders){
            Date date = DateUtil.to(order.end);
            int num;
            if(count == 30){
                num = date.getDate()-1;
            }else{
                num = date.getMonth();
            }
            datas.set(num,new Float(datas.get(num).floatValue()+order.price));
        }

        return datas;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
